package com.jse.security.sign;

import org.junit.Test;

import java.security.*;

/**
 * 签名验签测试
 * @author : zenghua
 * @version : v0.1 SignTest.java
 * @since : 2017/7/26 14:34
 */
public class SignTest {

    /**
     * 私钥加密，公钥验签
     */
    @Test
    public void testSignWithJdk() {
        try {
            KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
            gen.initialize(2048);
            KeyPair pair = gen.generateKeyPair();
            //rsa生成一对公私钥
            PublicKey publicKey  = pair.getPublic();
            PrivateKey privateKey  = pair.getPrivate();
            //SHA1withRSA算法进行签名
            Signature sign = Signature.getInstance("SHA1withRSA");
            sign.initSign(privateKey);
            byte[] data = "sss".getBytes();
            //更新用于签名的数据
            sign.update(data);
            byte[] signature = sign.sign();


            Signature verifySign = Signature.getInstance("SHA1withRSA");
            verifySign.initVerify(publicKey);
            //用于验签的数据
            verifySign.update(data);
            boolean flag = verifySign.verify(signature);
            System.out.println(flag);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void testSignWithOpenssl() {

    }
}
